package com.lc.afbx.adapters;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.GetJiFenShangCheng;
import com.lc.afbx.dialog.AlertDialog;
import com.lc.afbx.interf.ExchangeGoodsInterf;
import com.zcx.helper.util.UtilToast;

import java.util.List;

/**
 * Created by Administrator on 2016/5/28.
 */
public class AdapterJFShangCheng extends BaseAdapter {

    private List<GetJiFenShangCheng.Data> dataList;
    private LayoutInflater inflater;
    private Context context;
    private ExchangeGoodsInterf goodsInterf;

    public AdapterJFShangCheng(Context context, List<GetJiFenShangCheng.Data> dataList) {
        inflater = LayoutInflater.from(context);
        this.dataList = dataList;
        this.context = context;
        goodsInterf = (ExchangeGoodsInterf) context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_jifenshangcheng, null);
            BaseApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);

            holder.img = (SimpleDraweeView) convertView.findViewById(R.id.img_img_jifenshangcheng);
            holder.txtGoodName = (TextView) convertView.findViewById(R.id.txt_goodname_jifenshangcheng);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.txt_price_jifenshangcheng);
            holder.txtBtn = (TextView) convertView.findViewById(R.id.btn_duihuan_jifenshangcheng);
            holder.item_jifenshangcheng_tilte_color_tv = (TextView) convertView.findViewById(R.id.item_jifenshangcheng_tilte_color_tv);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.txtPrice.setText(dataList.get(position).integral);
        holder.txtGoodName.setText(dataList.get(position).goodsname);

        holder.img.setImageURI(Uri.parse(dataList.get(position).picurl));

        if (position == 1 || position == 0) {

            holder.item_jifenshangcheng_tilte_color_tv.setVisibility(View.VISIBLE);

        } else {

            holder.item_jifenshangcheng_tilte_color_tv.setVisibility(View.GONE);

        }


        holder.txtBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog dialog = new AlertDialog(context, "是否兑换" + dataList.get(position).goodsname) {
                    @Override
                    protected void onConfirm() {
                        goodsInterf.exchangeGoodsListener(dataList.get(position).id);
                    }

                    @Override
                    protected void onCancle() {

                    }
                };

                dialog.show();

//                new AlertDialog.Builder(context).setMessage("是否兑换" + dataList.get(position).goodsname).setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                }).show();
            }
        });
        return convertView;
    }

    class ViewHolder {

        SimpleDraweeView img;
        TextView txtGoodName, txtPrice, txtBtn, item_jifenshangcheng_tilte_color_tv;

    }
}
